<?php
function sellerLog($log_info){
    $log_admin_id=empty(session('admin_id')) ? 0 : session('admin_id');
	if($type==1){
		$seller =[
			'seller_id'=>0,
			'seller_name'=>'总后台',
			'store_id'=>0,
			__ACTION__=>'/Admin/Admin/login'
		];
	}else{
		$seller = session('seller');
	}
    $add['log_time'] = time();
    $add['log_seller_id'] = $seller['seller_id'];
    $add['log_admin_id'] = $log_admin_id;
    $add['log_seller_name'] = $seller['seller_name'];
    $add['log_content'] = $log_info;
    $add['log_seller_ip'] = getIP();
    $add['log_store_id'] = $seller['store_id'];
    $add['log_url'] = __ACTION__;
    M('seller_log')->add($add);
}


function getAdminInfo($admin_id){
	return D('admin')->where("admin_id=$admin_id")->find();
}

function tpversion()
{     
    if(!empty($_SESSION['isset_push']))
        return false;
    $_SESSION['isset_push'] = 1;    
    error_reporting(0);//关闭所有错误报告
    $app_path = dirname($_SERVER['SCRIPT_FILENAME']).'/';
    $version_txt_path = $app_path.'/Application/Admin/Conf/version.txt';
    $curent_version = file_get_contents($version_txt_path);
    
    $vaules = array(            
            'domain'=>$_SERVER['HTTP_HOST'], 
            'last_domain'=>$_SERVER['HTTP_HOST'], 
            'key_num'=>$curent_version, 
            'install_time'=>INSTALL_DATE, 
            'cpu'=>'0001',
            'mac'=>'0002',
            'serial_number'=>SERIALNUMBER,
            );     
     $url = "http://v1.weixinfenxiaoshop.com/index.php?m=Home&c=Index&a=user_push&".http_build_query($vaules); // 检测版本升级
     stream_context_set_default(array('http' => array('timeout' => 3)));
     file_get_contents($url);
}
 
/**
 * 面包屑导航  用于后台管理
 * 根据当前的控制器名称 和 action 方法
 */
function navigate_admin()
{        
    $navigate = include APP_PATH.'Common/Conf/navigate.php';    
    $location = strtolower('Seller/'.CONTROLLER_NAME);
    $arr = array(
        '后台首页'=>'javascript:void();',
        $navigate[$location]['name']=>'javascript:void();',
        $navigate[$location]['action'][ACTION_NAME]=>'javascript:void();',
    );
    return $arr;
}

/**
 * 导出excel
 * @param $strTable	表格内容
 * @param $filename 文件名
 */
function downloadExcel($strTable,$filename)
{
	header("Content-type: application/vnd.ms-excel");
	header("Content-Type: application/force-download");
	header("Content-Disposition: attachment; filename=".$filename."_".date('Y-m-d').".xls");
	header('Expires:0');
	header('Pragma:public');
	echo '<html><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />'.$strTable.'</html>';
}

/**
 * 格式化字节大小
 * @param  number $size      字节数
 * @param  string $delimiter 数字和单位分隔符
 * @return string            格式化后的带单位的大小
 */
function format_bytes($size, $delimiter = '') {
	$units = array('B', 'KB', 'MB', 'GB', 'TB', 'PB');
	for ($i = 0; $size >= 1024 && $i < 5; $i++) $size /= 1024;
	return round($size, 2) . $delimiter . $units[$i];
}

/**
 * 根据id获取地区名字
 * @param $regionId id
 */
function getRegionName($regionId){
    $data = M('region')->where(array('id'=>$regionId))->field('name')->find();
    return $data['name'];
}

function respose($res){
	header('Content-type:text/json');
	exit(json_encode($res));
}

function getMenuList($fix_count) {
	$menu_list = array(
			'order' => array('name' => '订单管理', 'icon'=>'fa-money', 'child' => array(
					array('name' => '全部订单', 'act'=>'index', 'op'=>'Order'),
					array('name' => '<span style="color:#fb6e52">待发货订单</span>', 'act'=>'index?order_status=0', 'op'=>'Order'),
					array('name' => '备货订单','act'=>'index?order_status=4','op'=>'Order'),
					array('name' => '已发货订单', 'act'=>'index?order_status=1', 'op'=>'Order'),
					array('name' => '已完成订单', 'act'=>'index?order_status=2', 'op'=>'Order'),
					array('name' => '取消/作废单', 'act'=>'index?order_status=3', 'op'=>'Order'),
					// array('name' => '发货单列表', 'act'=>'delivery_list', 'op'=>'Order'),   
					// array('name' => '发货设置', 'act'=>'index', 'op'=>'Plugin'),
					//array('name' => '运单模板', 'act'=>'store_waybill', 'op'=>'waybill_manage'),
					// array('name' => '商品评论','act'=>'index','op'=>'Comment'),
                    //array('name' => '部分退货订单','act'=>'return_list','op'=>'Order'),
					array('name' => '退货明细','act'=>'tuihuo_list','op'=>'Order'),
					array('name' => '<span style="color:#ef1313">异常订单</span>', 'act'=>'unusual', 'op'=>'Finance'),
					array('name' => '<span style="color:#ff0000">维修/换货明细('.$fix_count.')</span>','act'=>'fix_list','op'=>'Order'),
					//array('name' => '商品咨询','act'=>'ask_list','op'=>'Comment'), <span style="color:red">NEW新功能</span> 
			)),
            'user' => array('name' => '用户管理', 'icon'=>'fa-tasks', 'child' => array(
                array('name' => '用户列表', 'act'=>'index', 'op'=>'User'), ///index.php/Seller/goods/addEditGoods.html'
            )),
			'goods' => array('name' => '商品管理', 'icon'=>'fa-tasks', 'child' => array(
					array('name' => '发布新商品', 'act'=>'addEditGoods', 'op'=>'Goods'), ///index.php/Seller/goods/addEditGoods.html'
					//array('name' => '淘宝导入', 'act'=>'import', 'op'=>'index'),             //临时屏蔽淘宝商品导入
					array('name' => '店铺商品分类', 'act'=>'goods_class', 'op'=>'Store'),
					array('name' => '商品规格类型', 'act'=>'goodsTypeList', 'op'=>'GoodsTianxin'),
					array('name' => '<span style="color:#fb6e52">出售中的商品</span>', 'act'=>'goodsList?goods_state=1', 'op'=>'Goods'),
					array('name' => '待审核的商品', 'act'=>'goodsList?goods_state=0', 'op'=>'Goods'),
					//array('name' => '审核失败商品', 'act'=>'goodsList?goods_state=2', 'op'=>'Goods'),
					array('name' => '违规下架商品', 'act'=>'goodsList?goods_state=3', 'op'=>'Goods'),
				// 	array('name' => '活动商品', 'act'=>'promGoodsList', 'op'=>'Goods'),
					array('name' => '商品回收站', 'act'=>'trash', 'op'=>'Goods'),//商品回收站
					// array('name' => '关联版式', 'act'=>'store_plate', 'op'=>'index'),
					// array('name' => '商品规格', 'act' =>'specList', 'op' => 'GoodsTianxin'),
     //                array('name' => '品牌申请', 'act'=>'brandList', 'op'=>'Goods'),                            
					// array('name' => '图片空间', 'act'=>'store_album', 'op'=>'album_cate'),
			)),
/* 			'system' => array('name'=>'系统设置','icon'=>'fa-cog','child'=>array(
					array('name'=>'网站设置','act'=>'index','op'=>'System'),
			)), */
			'promotion' => array('name' => '活动促销', 'icon'=>'fa-bell', 'child' => array(
					// array('name' => '抢购管理', 'act'=>'flash_sale', 'op'=>'Promotion'),
					// array('name' => '团购管理', 'act'=>'group_buy_list', 'op'=>'Promotion'),
					array('name' => '店铺活动', 'act'=>'prom_goods_list', 'op'=>'Promotion'),
				// 	array('name' => '平台活动', 'act'=>'join_goods_list', 'op'=>'Promotion'),
                    array('name' => '每日秒杀', 'act'=>'daily_goods_list', 'op'=>'Promotion'),
                    // array('name' => '店铺满赠送礼', 'act'=>'full_reduction', 'op'=>'Promotion'),
                    array('name' => '店铺满赠送礼', 'act'=>'prom_full_list', 'op'=>'Promotion'),
					// array('name' => '商品促销', 'act'=>'prom_goods_list', 'op'=>'Promotion'),
					// array('name' => '订单促销', 'act'=>'prom_order_list', 'op'=>'Promotion'),
					 array('name' => '店铺优惠券', 'act'=>'index', 'op'=>'Coupon'),
					//array('name' => '分销管理', 'act'=>'store_activity', 'op'=>'promotion'),
			)),
			'store' => array('name' => '店铺管理', 'icon'=>'fa-cog', 'child' => array(
					array('name' => '店铺设置', 'act'=>'store_setting', 'op'=>'Store'),
					//array('name' => '店铺装修', 'act'=>'store_decoration', 'op'=>'Store'),
					//array('name' => '店铺导航', 'act'=>'navigation_list', 'op'=>'Store'),
					//array('name' => '经营类目', 'act'=>'bind_class_list', 'op'=>'Store'),
					//array('name' => '店铺信息', 'act'=>'store_info', 'op'=>'Store'),
					// array('name' => '店铺分类', 'act'=>'goods_class', 'op'=>'Store'),
					array('name' => '打印设置', 'act'=>'printer', 'op'=>'Store'),
					
			)),
			// 'account' => array('name' => '账号管理', 'icon'=>'fa-home', 'child' => array(
			// 		array('name' => '账号列表', 'act'=>'index', 'op'=>'Admin'),
			// 		array('name' => '账号组', 'act'=>'role', 'op'=>'Admin'),
			// 		//array('name' => '账号日志', 'act'=>'log', 'op'=>'Admin'),
			// 		//array('name' => '店铺消费', 'act'=>'store_cost', 'op'=>'cost_list'),
			// )),
			'finance' => array('name' => '财务管理', 'icon'=>'fa-book', 'child' => array(
					array('name' => '结算中心', 'act'=>'withdrawals', 'op'=>'Finance'),
					// array('name' => '汇款记录', 'act'=>'remittance', 'op'=>'Finance'),
                    array('name' => '结算记录', 'act'=>'order_statis', 'op'=>'Finance'),
					array('name' => '待结算订单', 'act'=>'order_statis_djiesuan?type=1', 'op'=>'Finance'),
					array('name' => '已结算订单', 'act'=>'order_statis_djiesuan?type=2', 'op'=>'Finance'),
					array('name' => '平台退货产品','act'=>'tuihuo_list','op'=>'Order'),
					array('name' => '配送订单记录','act'=>'driver_list','op'=>'Order'),
					// array('name' => '平台未收款订单', 'act'=>'order_no_statis', 'op'=>'Finance'),
					// array('name' => '平台已收款订单', 'act'=>'order_yes_statis', 'op'=>'Finance'),
					// array('name' => '分成日志', 'act'=>'rebate_log', 'op'=>'Distribut'),
		    )),
			//'statistics' => array('name' => '统计报表 <span style="color:red">NEW新功能</span>', 'icon'=>'fa-signal', 'child' => array(
			'statistics' => array('name' => '统计报表', 'icon'=>'fa-signal', 'child' => array(
					array('name' => '店铺概况', 'act'=>'index', 'op'=>'Report'),
					array('name' => '商品分析', 'act'=>'saleTop', 'op'=>'Report'),
					//array('name' => '运营报告', 'act'=>'finance', 'op'=>'Report'),
					array('name' => '商品入库明细', 'act'=>'saleTop_info', 'op'=>'Report'),
					array('name' => '客户加购产品', 'act'=>'jiagou', 'op'=>'Report'),
				// 	array('name' => '用户消费', 'act'=>'userTop', 'op'=>'Report'),
					//array('name' => '流量统计', 'act'=>'visit', 'op'=>'Report'),
			)),
		    /* 
			'distribut' => array('name' => '分销管理', 'icon'=>'fa-cubes', 'child' => array(
					array('name' => '分销设置', 'act'=>'set', 'op'=>'Distribut'),
					array('name' => '分销商列表', 'act'=>'distributor_list', 'op'=>'Distribut'),
					array('name' => '分销关系', 'act'=>'tree', 'op'=>'Distribut'),
				 	array('name' => '提现申请', 'act'=>'withdrawals', 'control'=>'Distribut'), 
					array('name' => '分成日志', 'act'=>'rebate_log', 'op'=>'Distribut'),
			)), */			
            
	);
	//require './Application/Admin/Common/function_extra.php';
	//$rarr=getMenuList_seller_extra($menu_list);
	return $menu_list;
}


function getAdlink(){
    $menuarr=	array(
        "/pages/index/index"=>"商城首页",
        "/pages/goods_details/goods_details" => "商品详情",
        "/pages/store/index" => "店铺分类",
    );
    return $menuarr;
}

function pic64($imgs){
$img = file_get_contents($imgs);
$data = base64_encode($img);
return 'data:image/png;base64,'.$data;
}

function cart_price($goods_id=0){ //更新购物车
			$goods=M('cart')->where('goods_id='.$goods_id)->select();//更新购物车
			
			foreach($goods as $v){
			$new_price=get_goods_huodong($v['goods_id'],$v['spec_key']);
				if($new_price){
					M('cart')->where('goods_id='.$v['goods_id'].' and `spec_key` = "'.$v['spec_key'].'"')->setField([
					'spec_key_name'=>$new_price["key_name"],
					'goods_name'=>$new_price["goods_name"],
					'goods_price'=>sanwei($new_price['price']),
					'member_goods_price'=>sanwei($new_price['price'])
					]);
				}else{
					M('cart')->where('goods_id='.$v['goods_id'].' and `spec_key` = "'.$v['spec_key'].'"')->delete();
				}
			}
}

function sellercart_update($store_id){ //商家购物车活动同步
	$nowtime=time()-60 * 60 * 24 * 5;
	//更新活动购物车价格
	$goods=M('cart')->field('goods_id,prom_id,spec_key')->where('add_time>'.$nowtime.' and store_id='.$store_id)->select();

	foreach($goods as $vs){
			$new_jiage=get_goods_huodong($vs['goods_id'],$vs['spec_key']);
			M('cart')->where('goods_id='.$vs['goods_id'].' and `spec_key` = "'.$vs['spec_key'].'"')->setField([
			'spec_key_name'=>$new_jiage["key_name"],
			'goods_price'=>sanwei($new_jiage["price"]),
			'member_goods_price'=>sanwei($new_jiage["price"]),
			'prom_msg'=>$new_jiage["prom_msg"],
			'prom_id'=>$new_jiage["prom_id"]
		]);
	}
}